/*
* GeoVISTA Center (Penn State, Dept. of Geography)
* Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
* All Rights Researved.
*
*
* @author: jin Chen
* @date: Jul 10, 2003$
* @version: 1.0
*/
package edu.psu.geovista.app.pcp.plot.control;

import edu.psu.geovista.app.pcp.plot.control.gui.AxisPlaceControl;
import edu.psu.geovista.app.pcp.plot.control.gui.AxisSizeSection;
import edu.psu.geovista.app.pcp.plot.control.tools.AxisMoveTool;
import edu.psu.geovista.app.pcp.plot.control.tools.AxisPlaceTool;
import edu.psu.geovista.app.pcp.plot.control.tools.BasicTool;
import edu.psu.geovista.app.plot.axis.AxisList;
import edu.psu.geovista.app.plot.axis.XAxisOrder;
import edu.psu.geovista.app.plot.axis.YAxis;
import edu.psu.geovista.data.model.MetaData;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;

public class AxisLocationCfg extends BasicCfg{

    AxisList yaxisList;
    XAxisOrder xaxis;
    AxisPlaceTool axisPlCmd;
    HashSet dataNameList;

    protected Map columndescription;

    protected JPanel controlP=new JPanel();

    //private AxisPlaceControl placeControlP;
    protected  AxisPlaceControl axisPlaceControl;
    protected  AxisSizeSection  axisSizeP;


    public AxisLocationCfg(ConfigManager owner) {
        if (owner!=null){
            this.owner =owner;
            this.axisPlCmd=(AxisPlaceTool)owner.getCmd(BasicTool.AxisPlaceT );
            yaxisList=owner.getTargetUI().getPlot().getYaxisList() ;
            xaxis=owner.getTargetUI().getPlot().getXaxis() ;
            edu.psu.geovista.data.model.AppDataModel dataModel=(edu.psu.geovista.data.model.AppDataModel) owner.getTargetUI().getPlot().getDataModel() ;
            java.util.List an=dataModel.getColumnNameList() ;
            dataNameList=new HashSet(an);

            MetaData meta = dataModel.getMetaData();
            if(meta!=null)
                this.columndescription = meta.getColumndescription();
            else
                columndescription=new Hashtable();


        }
        this.addComponentListener(this);
        initComponents();
       // initValue();

    }
    /*******************************************************************************************************
     *                service
     *******************************************************************************************************/
    /**
     * select a variable in right table
     * @param varname
     */
    public void selectVariable(String varname){
        this.axisPlaceControl.selectInRightTable(varname);
    }


    /*******************************************************************************************************
     *                support
     *******************************************************************************************************/
    /**
     *  
     */
    public void updateView() {
        //initValue();
        String  varname =null;
        if(axisPlaceControl!=null)
                 varname=axisPlaceControl.getSelectedInRightTable();//A variable may already be selected before the page showing, need keep it
        if(axisPlaceControl!=null){ //with timeseries it is null
        // 
        axisPlaceControl.initRightTable();
        if(varname!=null)
            axisPlaceControl.selectInRightTable(varname);
        }
    }
    protected void initValue() {
        //this.axisSizeP.initValue(this);
       /* //textfields
        YAxis axis0=yaxisList.getAxis(0);
        if(axis0!=null){
            this.axisHeightFd.setText(axis0.getActiveSize()+"" );
        }
        this.axisIntervalFd.setText(xaxis.getDefaultInterval() +"");

*/

    }
    /**************************************************
     *                properties
     ***************************************************/
    public AxisList getYaxisList() {
        return yaxisList;
    }

    public AxisPlaceTool getAxisPlCmd() {
        return axisPlCmd;
    }

    public Map getColumndescription() {
        return columndescription;
    }

    public HashSet getDataNameList() {
        return dataNameList;
    }

    public XAxisOrder getXaxis() {
        return xaxis;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    protected  void initComponents() {

        //North Panel
        this.axisSizeP=new AxisSizeSection(this);

        //Center Panel
        setupAxisPlaceControl();
        axisPlaceControl.setBorder(new javax.swing.border.TitledBorder(null, "Axes Location", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("MS Sans Serif", 1, 14)));
        axisPlaceControl.repaint() ;

         //South panel
         JButton applybtn =new JButton("Apply");
                    applybtn.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                  axisPlaceControl.apply();
                            }
                    });
        controlP.setLayout(new FlowLayout(FlowLayout.RIGHT ) );
        this.controlP.add(applybtn);
        JButton closebtn=new JButton("Close");
                    closebtn.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                AxisLocationCfg.this.getOwner().close();
                            }
                    });
         this.controlP.add(closebtn);

        //add all
        this.setLayout(new BorderLayout());
        this.add(axisSizeP, BorderLayout.NORTH );
        this.add(axisPlaceControl);//, BorderLayout.NORTH );
        this.add(controlP,BorderLayout.SOUTH );


    }

    protected void setupAxisPlaceControl() {
        axisPlaceControl = new AxisPlaceControl(this){

            public String[] getSelectedItemNames() throws Exception {
                return new String[0];  //To change body of implemented methods use File | Settings | File Templates.
            }

            public   void apply() {
                super.apply();
                initValue() ;
            }
            //call
            protected boolean trySetDisplayType(YAxis axis, String type) {
                return AxisLocationCfg.this.trySetDisplayType(axis,type);
            }
        };//inner class
    }







    public boolean trySetDisplayType(YAxis axis,String type){
        String curType=axis.getDisplayType() ;
        if(curType.equals(YAxis.DTYPE_Qty )&&type.equals(YAxis.DTYPE_Ord ) ){
            axis.setDisplayType(type);
            this.axisPlCmd.repaintAxisLayer();
            this.axisPlCmd.repaintPlot() ;
            return true;
        }
        else if(curType.equals(YAxis.DTYPE_Ord )&&type.equals(YAxis.DTYPE_Qty )){

            float colv=axis.getColumnValue() ;
            int x=(int) xaxis.getGraphicValueOf(colv);
            int deltaX=x-axis.getLocation().x ;
            AxisMoveTool axm=(AxisMoveTool) this.owner.getCmd(BasicTool.AxisMoveT );
            axm.moveAxis(axis,deltaX);
            axis.setDisplayType(type);//must after move,otherwise unmovable
            this.axisPlCmd.repaintAxisLayer(); //change the view of the axis
            this.axisPlCmd.repaintPlot() ; //change the view of the axis
            return true;
        }
        return false;
    }


    public AxisSizeSection getAxisSizeP() {
        return axisSizeP;
    }

}
